package com.base.service.impl;

import java.util.List;

import com.base.service.PermissionService;
import com.common.bean.Menuctrl;
import com.common.bean.MenuctrlId;
import com.common.bean.Personneltorulelink;
import com.common.bean.PersonneltorulelinkId;
import com.common.util.AbstractManager;

public class PermissionServiceImpl extends AbstractManager implements PermissionService {

	public void addMenuctrl(long menuid, long ruleno) {
		Menuctrl menuctrl = new Menuctrl();
		MenuctrlId id = new MenuctrlId();
		id.setMenuid(menuid);
		id.setRuleno(ruleno);
		menuctrl.setId(id);
		
		this.getHibernateTemplate().saveOrUpdate(menuctrl);
	}

	public void removeMenuctrl(long menuid, long ruleno) {
		Menuctrl menuctrl = new Menuctrl();
		MenuctrlId id = new MenuctrlId();
		id.setMenuid(menuid);
		id.setRuleno(ruleno);
		
		menuctrl = (Menuctrl) this.getHibernateTemplate().load(Menuctrl.class, id);
		this.getHibernateTemplate().delete(menuctrl);
	}

	public void addPersonneltorulelink(long ruleno, String staffcode) {
		Personneltorulelink personneltorulelink = new Personneltorulelink();
		PersonneltorulelinkId id = new PersonneltorulelinkId();
		id.setRuleno(ruleno);
		id.setStaffcode(staffcode);
		personneltorulelink.setId(id);
		
		this.getHibernateTemplate().saveOrUpdate(personneltorulelink);
	}

	public List<Menuctrl> findAllMenuctrl() {
		return null;
	}

	public List<Personneltorulelink> findAllPersonneltorulelink() {
		return null;
	}

	public Menuctrl findMenuctrl(long menuid, long ruleno) {
		Menuctrl menuctrl = new Menuctrl();
		MenuctrlId id = new MenuctrlId();
		id.setMenuid(menuid);
		id.setRuleno(ruleno);
		
		menuctrl = (Menuctrl) this.getHibernateTemplate().load(Menuctrl.class, id);
		return menuctrl;
	}

	public List findMenuctrlByRules(long ruleno) {
		String sql = "select menuid , ruleno from MENUCTRL where ruleno = " + ruleno;
		return this.getSession().createSQLQuery(sql).list();
	}

	public List findPersonneltoruleByStaffcode(String staffcode) {
		String sql = "select ruleno , staffcode from PERSONNELTORULELINK where staffcode = " + staffcode;
		return this.getSession().createSQLQuery(sql).list();
	}

	public Personneltorulelink findPersonneltorulelink(long ruleno, String staffcode) {
		Personneltorulelink personneltorulelink = new Personneltorulelink();
		String hql = "from Personneltorulelink pr where pr.id.staffcode = ? and pr.id.ruleno = ?";
		personneltorulelink = (Personneltorulelink) this.getHibernateTemplate().find(hql,new Object[]{staffcode,ruleno}).get(0);
		return personneltorulelink;
	}

	public void removePersonneltorulelink(long ruleno, String staffcode) {
		Personneltorulelink personneltorulelink = new Personneltorulelink();
		PersonneltorulelinkId id = new PersonneltorulelinkId();
		id.setRuleno(ruleno);
		id.setStaffcode(staffcode);
		
		personneltorulelink = (Personneltorulelink) this.getHibernateTemplate().load(Personneltorulelink.class, id);
		this.getHibernateTemplate().delete(personneltorulelink);
	}

}
